(function() {
  define(function(require, exports, module) {
    var MyScroll = function(options) {
      this.wrapperId = options.wrapperId;
      this.pullUpId = options.pullUpId;
      this.pullDownId = options.pullDownId;
      this.pullUpAction = options.pullUpAction;
      this.pullDownAction = options.pullDownAction;
      this.stopScroll = options.stopScroll;
      this.wrapperTop = options.wrapperTop;
      var that = this;

      this.pullDownEl = document.getElementById(this.pullDownId);
      if (this.pullDownEl) {
        this.pullDownOffset = this.pullDownEl.offsetHeight;
      } else {
        this.pullDownOffset = 0;
      }
      this.pullUpEl = document.getElementById(this.pullUpId);
      if (this.pullUpEl) {
        this.pullUpOffset = this.pullUpEl.offsetHeight;
      } else {
        this.pullUpOffset = 0;
      }
      if(this.wrapperTop){
        document.getElementById(that.wrapperId).style.top = this.wrapperTop;
      }
      if(this.stopScroll){
        this.pullDownEl.style.display = 'none';
        this.pullUpEl.style.display = 'none';
        setTimeout(function() { document.getElementById(that.wrapperId).style.left = '0'; }, 1);
        return false;
      }
      this.createIScroll();
      setTimeout(function() { document.getElementById(that.wrapperId).style.left = '0'; }, 1);
    }
    MyScroll.prototype.createIScroll = function() {
      var self = this;
      var myOptions = {
        mouseWheel: true,
        scrollbars: true,
        fadeScrollbars: true,
        preventDefault: false,
        probeType: 1,
        click: true,
        startY: -self.pullDownOffset
      };

      var hasCls = function(element, csName) {
        return element.className.match(RegExp('(\\s|^)' + csName + '(\\s|$)'));
      };

      var addCls = function(element, csName) {
        if (!hasCls(element, csName)) {
          element.className += ' ' + csName;
        }
      };
      var removeCls = function(element, csName) {
        if (hasCls(element, csName)) {
          var reg = new RegExp('(\\s|^)' + csName + '(\\s|$)');
          element.className = element.className.replace(reg, ' ');
        }
      };

      self.scrollInst = new IScroll("#" + self.wrapperId, myOptions);
      console.log("maxScrollY-1:" + self.scrollInst.maxScrollY);

      _maxScrollY = self.scrollInst.maxScrollY = self.scrollInst.maxScrollY + self.pullUpOffset;
      console.log("maxScrollY-2:" + self.scrollInst.maxScrollY)

      var isScrolling = false;

      //Event: scrollStart
      self.scrollInst.on("scrollStart", function() {
        if (this.y == this.startY) {
          isScrolling = true;
        }
        //TODO
        console.log("start-y:" + this.y);
      });

      //Event: scroll
      self.scrollInst.on("scroll", function() {
        // if (this.y > 5 && !self.pullDownEl.className.match('flip')) {
        if (this.y > 5 && !hasCls(self.pullDownEl, 'flip')) {
          removeCls(self.pullDownEl, 'flip');
          addCls(self.pullDownEl, 'flip');
          // self.pullDownEl.className = 'flip';
          self.pullDownEl.querySelector('.pullDownLabel').innerHTML = '释放刷新...';
          this.minScrollY = 0;
        // } else if (this.y < 5 && self.pullDownEl.className.match('flip')) {
        } else if (this.y < 5 && hasCls(self.pullDownEl, 'flip')) {
          removeCls(self.pullDownEl, 'c-loading');
          self.pullDownEl.querySelector('.pullDownLabel').innerHTML = '下拉刷新...';
          this.minScrollY = -self.pullDownOffset;
        } else if (this.y <= (_maxScrollY - self.pullUpOffset) && !hasCls(self.pullUpEl, 'flip')) {
          removeCls(self.pullUpEl, 'flip');
          addCls(self.pullUpEl, 'flip');
          self.pullUpEl.querySelector('.pullUpLabel').innerHTML = '释放刷新...';
          this.maxScrollY = this.maxScrollY - self.pullUpOffset;
        } else if (this.y > (_maxScrollY - self.pullUpOffset) && hasCls(self.pullUpEl, 'flip')) {
          removeCls(self.pullUpEl, 'c-loading');
          self.pullUpEl.querySelector('.pullUpLabel').innerHTML = '上拉加载更多...';
          this.maxScrollY = this.maxScrollY + self.pullUpOffset;
        }

        //TODO
        console.log("y:" + this.y);
      });

      //Event: scrollEnd
      self.scrollInst.on("scrollEnd", function() {
        console.log("scroll end");
        console.log("directionY:" + this.directionY);
        console.log("y1:" + this.y);
        console.log("maxScrollY-3:" + this.maxScrollY);
        if (self.pullDownEl && !hasCls(self.pullDownEl, 'flip') && this.y > this.options.startY) {
          console.log("resume");
          this.scrollTo(0, this.options.startY, 800);
        } else if (self.pullDownEl && hasCls(self.pullDownEl, 'flip')) {
          removeCls(self.pullDownEl, 'c-loading');
          addCls(self.pullDownEl, 'c-loading');
          self.pullDownEl.querySelector(".pullDownLabel").innerHTML = "加载中...";
          // Execute custom function (ajax call?)
          if (isScrolling) {
            console.log("before pull down action:" + this.y);
            self.pullDownAction();
            console.log("after pull down action:" + this.y);
          }
        } else if (self.pullUpEl && hasCls(self.pullUpEl, 'flip')) {
          console.log("pull up loading");
          removeCls(self.pullUpEl, 'c-loading');
          addCls(self.pullUpEl, 'c-loading');
          self.pullUpEl.querySelector(".pullUpLabel").innerHTML = "加载中...";
          // Execute custom function (ajax call?)
          if (isScrolling) {
            console.log("before pull up action:" + this.y);
            self.pullUpAction();
            console.log("after pull up action:" + this.y);
          }
        }
        isScrolling = false;
      });
      //Event: refresh
      self.scrollInst.on("refresh", function() {
        console.log("maxScrollY-4:" + this.maxScrollY);
        _maxScrollY = this.maxScrollY = this.maxScrollY + self.pullUpOffset;
        console.log("maxScrollY-5:" + this.maxScrollY);

        if (self.pullDownEl && hasCls(self.pullDownEl, 'c-loading')) {
          removeCls(self.pullDownEl, 'c-loading');
          removeCls(self.pullDownEl, 'flip');
          console.log('remove all class');
          self.pullDownEl.querySelector(".pullDownLabel").innerHTML = "下拉刷新";
          this.scrollTo(0, this.options.startY, 0);
        } else if (self.pullUpEl && hasCls(self.pullUpEl, 'c-loading')) {
          removeCls(self.pullUpEl, 'c-loading');
          removeCls(self.pullUpEl, 'flip');
          self.pullUpEl.querySelector(".pullUpLabel").innerHTML = "上拉加载更多";
          this.scrollTo(0, this.maxScrollY, 0);
        }
        console.log("refresh finished!");
      });
    }
    return MyScroll;
  });
}).call(this);
